package com.kgis.fm.common.component{
   
   import com.kgis.fm.common.constant.IconConstants;
   
   import flash.events.Event;
   import flash.events.MouseEvent;
   
   import mx.controls.LinkButton;
   import mx.controls.TextInput;
   import mx.events.FlexEvent;

   public class RemovableTextInput extends TextInput {

      public var removeButton:LinkButton;

      private var _removable:Boolean = true;
      public function get removable():Boolean {
         return _removable;
      }
      public function set removable(value:Boolean):void {
         _removable = value;
      }

      public function RemovableTextInput() {
         super();
         styleName = "queryInput";
         setStyle("paddingRight", 17);
         percentWidth = 100;
         height = 22;
         addEventListener(FlexEvent.VALUE_COMMIT, onChange);
         addEventListener(Event.CHANGE, onChange);
      }

      override protected function createChildren():void {
         super.createChildren();
         if(removable && !removeButton) {
            removeButton = new LinkButton();
            removeButton.visible = removeButton.includeInLayout = false;
            removeButton.setStyle("icon", IconConstants.CROSS_ICON);
            removeButton.styleName = "removeButton";
            removeButton.y = 2;
            removeButton.width = 16;
            removeButton.height = 16;
            removeButton.addEventListener(MouseEvent.CLICK, onRemoveContent);
         }

         if(removeButton){
            addChild(removeButton);
         }
      }

      private function onChange(e:*):void {
         if(removeButton){
            removeButton.visible = text != "";
            removeButton.includeInLayout = text != "";
         }
      }

      private function onRemoveContent(e:MouseEvent):void {
         text = "";
         toolTip = null;
         e.stopPropagation();
      }

      override protected function updateDisplayList(unscaledWidth:Number,
                                                    unscaledHeight:Number):void {
         super.updateDisplayList(unscaledWidth, unscaledHeight);
         if(removeButton) {
            removeButton.setActualSize(18, 18);
            removeButton.x = unscaledWidth - 20;
         }
      }
   }
}